<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>设计模式（2）——工厂模式详解 | 博客主页 | 世界是个球，前方总有路！</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="设计模式">
    <meta name="description" content="一、前言&amp;emsp;&amp;emsp;今天这篇博客来介绍一下设计模式中非常常用的一种——工厂模式。事实上，工厂模式这个名称只是一个统称，它又分为简单工厂、工厂方法以及抽象工厂。接下来我就来分别介绍一下它们。   二、正文&amp;emsp;2.1 针对接口编程&amp;emsp;&amp;emsp;针对接口编程是面向对象程序设计语言中的一个设计原则，这指示我们在编程的过程中，应该尽量的使用接口去引用我们的具体对象，而不是使用具">
<meta property="og:type" content="article">
<meta property="og:title" content="设计模式（2）——工厂模式详解">
<meta property="og:url" content="http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/index.html">
<meta property="og:site_name" content="博客主页">
<meta property="og:description" content="一、前言&amp;emsp;&amp;emsp;今天这篇博客来介绍一下设计模式中非常常用的一种——工厂模式。事实上，工厂模式这个名称只是一个统称，它又分为简单工厂、工厂方法以及抽象工厂。接下来我就来分别介绍一下它们。   二、正文&amp;emsp;2.1 针对接口编程&amp;emsp;&amp;emsp;针对接口编程是面向对象程序设计语言中的一个设计原则，这指示我们在编程的过程中，应该尽量的使用接口去引用我们的具体对象，而不是使用具">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-29T09:54:40.000Z">
<meta property="article:modified_time" content="2020-03-29T13:41:43.273Z">
<meta property="article:author" content="特务依昂">
<meta property="article:tag" content="设计模式">
<meta name="twitter:card" content="summary">
    
        <link rel="alternate" type="application/atom+xml" title="博客主页" href="/blog/atom.xml">
    
    <link rel="shortcut icon" href="/blog/img/title.png">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">特务依昂</h5>
          <a href="mailto:1131564805@qq.com" title="1131564805@qq.com" class="mail">1131564805@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                博客
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/tewuyiang" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://weibo.com/u/5516635708/" target="_blank" >
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://www.cnblogs.com/tuyang1129/" target="_blank" >
                <i class="icon icon-lg icon-link"></i>
                博客园
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">设计模式（2）——工厂模式详解</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">设计模式（2）——工厂模式详解</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-03-29T09:54:40.000Z" itemprop="datePublished" class="page-time">
  2020-03-29
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#一、前言"><span class="post-toc-number">1.</span> <span class="post-toc-text">一、前言</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#二、正文"><span class="post-toc-number">2.</span> <span class="post-toc-text">二、正文</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#emsp-2-1-针对接口编程"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">&amp;emsp;2.1 针对接口编程</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#emsp-2-2-简单工厂"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">&amp;emsp;2.2 简单工厂</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#emsp-2-3-工厂方法模式"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">&amp;emsp;2.3 工厂方法模式</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#emsp-2-4-抽象工厂模式"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">&amp;emsp;2.4 抽象工厂模式</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#三、总结"><span class="post-toc-number">3.</span> <span class="post-toc-text">三、总结</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#四、参考"><span class="post-toc-number">4.</span> <span class="post-toc-text">四、参考</span></a></li></ol>
        </nav>
    </aside>


<article id="post-设计模式（2）——工厂模式详解"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">设计模式（2）——工厂模式详解</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-03-29 17:54:40" datetime="2020-03-29T09:54:40.000Z"  itemprop="datePublished">2020-03-29</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h2><p>&emsp;&emsp;今天这篇博客来介绍一下设计模式中非常常用的一种——工厂模式。事实上，工厂模式这个名称只是一个统称，它又分为简单工厂、工厂方法以及抽象工厂。接下来我就来分别介绍一下它们。</p>
<br>

<h2 id="二、正文"><a href="#二、正文" class="headerlink" title="二、正文"></a>二、正文</h2><h3 id="emsp-2-1-针对接口编程"><a href="#emsp-2-1-针对接口编程" class="headerlink" title="&emsp;2.1 针对接口编程"></a>&emsp;2.1 针对接口编程</h3><p>&emsp;&emsp;针对接口编程是面向对象程序设计语言中的一个设计原则，这指示我们在编程的过程中，应该尽量的使用接口去引用我们的具体对象，而不是使用具体的类，这么做的目的是让使用这个对象的代码不会过度依赖于具体的类型。如果我们使用的是接口，而不是具体的类型，那我们就为这里的代码保留了一些弹性，因为我们可以很简单地更换另一个子类，而不会影响使用它的代码。</p>
<blockquote>
<p><strong>注意：</strong>这里所说的接口并没有规定就是interface，它表示的是所有的父类型，可以是一个interface，可以是一个抽象类，甚至可以是一个普通类，它泛指某种类型的上层类型；</p>
</blockquote>
<p>&emsp;&emsp;而工厂方法的实现就是遵循了这个原则，从而实现了解耦。</p>
<br>

<h3 id="emsp-2-2-简单工厂"><a href="#emsp-2-2-简单工厂" class="headerlink" title="&emsp;2.2 简单工厂"></a>&emsp;2.2 简单工厂</h3><p>&emsp;&emsp;简单工厂和它的名字一样，实现起来非常简单，它的定义如下：</p>
<blockquote>
<p><strong>在创建对象时不向用户暴露创建对象的细节，仅仅提供一个通用的接口，通过这个接口来创建对象；</strong></p>
</blockquote>
<p>&emsp;&emsp;简单工厂将创建具有公共父类对象的代码单独拿出来，放入到了一个类中，并提供一个接口，可以通过调用这个接口来创建对象，但是具体创建的是哪一个子类对象由简单工厂决定。我们直接通过以下代码来理解。假设我们需要创建<code>A</code>类产品，而<code>A</code>类产品有四种具体的产品，如下所示：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// A类产品</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">A</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 以下均为具体产品</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A1</span> <span class="keyword">extends</span> <span class="title">A</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A2</span> <span class="keyword">extends</span> <span class="title">A</span></span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A3</span> <span class="keyword">extends</span> <span class="title">A</span></span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;假设上面三个具体产品的编号分别为<code>1、2、3</code>，我们在代码中需要根据编号来选择需要创建的产品，于是我们可以这样写：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> type = System.in.read();</span><br><span class="line">    A a = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (type == <span class="number">1</span>) &#123;</span><br><span class="line">        a = <span class="keyword">new</span> A1();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (type == <span class="number">2</span>) &#123;</span><br><span class="line">        a = <span class="keyword">new</span> A2();</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (type == <span class="number">3</span>) &#123;</span><br><span class="line">        a = <span class="keyword">new</span> A3();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">"no product"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;很明显，上面的代码是非常不好的，它会导致创建对象的代码与具体的对象类型有很大的耦合性。从代码复用的角度来说，假设我们在其他地方也需要创建<code>A</code>产品，则每一个需要使用的地方都要写上上面这段代码；再从扩展性的角度来说，假设我们需要新加入一个产品<code>A4</code>，或者修改产品的编号，那对于每一个使用了上面这段代码的地方都需要进行修改，大大增加了程序维护的难度，这就是高耦合的危害。所以我们需要对上面的代码进行解耦，让具体逻辑和创建对象的代码分离。根据我们编程的经验，可以这要进行：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AFactory</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> A <span class="title">createA</span><span class="params">(<span class="keyword">int</span> type)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (type == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> A1();</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (type == <span class="number">2</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> A2();</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (type == <span class="number">3</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> A3();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"no product"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;以上就是简单工厂的实现。我们将创建对象的过程提取到了一个类的公共方法中，方法的返回值就是这些产品的公共父类，这样我们在需要创建对象的地方直接调用上面的工厂即可。而且方法隐藏了对象的具体类型，降低了对具体类型的耦合。而且在需要修改时，只需要修改方法即可，对调用它的代码是隐蔽的。</p>
<p>&emsp;&emsp;实际上，<strong>简单工厂并不是一个设计模式</strong>，它是我们编码时的一个良好习惯——抽取重复使用的代码，便于复用。</p>
<br>

<h3 id="emsp-2-3-工厂方法模式"><a href="#emsp-2-3-工厂方法模式" class="headerlink" title="&emsp;2.3 工厂方法模式"></a>&emsp;2.3 工厂方法模式</h3><p>&emsp;&emsp;我们先来看看工厂方法的定义：</p>
<blockquote>
<p><strong>工厂方法模式定义了一个创建对象的接口，但是这个接口由子类进行实现，子类决定具体需要创建的对象。工厂方法将创建对象的工作延迟到了子类；</strong></p>
</blockquote>
<p>&emsp;&emsp;工厂方法首先定义了一个顶层的工厂类，它提供了一个创建对象的方法，但是这个方法一般没有具有实现，因为它也不确定需要创建哪一个对象。而具体创建对象的工作是交由子类工厂进行实现的。程序员根据子类工厂的职责，来实现父工厂创建对象的接口方法，由子类决定自己需要创建的具体类型。而选择不同的子类工厂，自然也会创建出不同的对象。下面我们通过代码来看一看。</p>
<p>&emsp;&emsp;假设我们现在需要创建<code>Product</code>类型的产品，而<code>Product</code>类型有两种具体的产品，而且<code>Product</code>类型的产品都有一个<code>show</code>操作，代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 抽象的产品</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Product</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 定义产品的公共方法</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 具体产品1</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Product1</span> <span class="keyword">extends</span> <span class="title">Product</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"Product1"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 具体产品2</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Product2</span> <span class="keyword">extends</span> <span class="title">Product</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"Product2"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;我们需要工厂<code>Factory</code>来创建这个产品，但是我们希望有多个工厂，每个工厂负责创建不同类型的产品。但是我们希望这些工厂在使用时没有任何区别，不想知道具体使用的是哪个工厂，只是希望在使用它时，能够给我创建需要的产品。于是我们可以创建一个公共的工厂类，但是它只是一个公共的标准，并不会工作，而真正工作的是基于这个标准建造的工厂，它们分别负责创建<code>Product1</code>和<code>Product2</code>：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 抽象的工厂类提供了一个创建对象的接口</span></span><br><span class="line"><span class="comment">// 它是具体工厂的一个标准</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Factory</span> </span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建对象的接口，但是没有实现，由子类来决定创建什么对象</span></span><br><span class="line"><span class="comment">     * 不需要在意产品的具体类型，因为它们都有公共的父类</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> <span class="keyword">public</span> Product <span class="title">createProduct</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 子类实现抽象的工厂类，实现具体的创建对象的接口</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Factory1</span> <span class="keyword">extends</span> <span class="title">Factory</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Product <span class="title">createProduct</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// Factory1创建Product1</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Product1();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Factory2</span> <span class="keyword">extends</span> <span class="title">Factory</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Product <span class="title">createProduct</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// Factory2创建Product2</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Product2();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;于是现在我们可以创建具体的对象了。我们选择不同的工厂，从而创建不同的产品：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 根据选择的工厂不同，创建的产品也不同</span></span><br><span class="line">    <span class="comment">// 但是不需要在意产品的具体类型，因为产品有公共的分类Product</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 选择工厂1</span></span><br><span class="line">    Factory f1 = <span class="keyword">new</span> Factory1();</span><br><span class="line">    Product p1 = f1.createProduct();</span><br><span class="line">    p1.show();  <span class="comment">// 执行Product1的show方法</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 选择工厂2</span></span><br><span class="line">    Factory f2 = <span class="keyword">new</span> Factory2();</span><br><span class="line">    Product p2 = f2.createProduct();</span><br><span class="line">    p2.show();  <span class="comment">// 执行Product2的show方法</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 执行结果</span></span><br><span class="line">Product1</span><br><span class="line">Product2</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;可以看到，根据我们创建的工厂不同，最后生产出的产品也不同，但是不论选用的是哪一个工厂，都可以使用<code>Factory</code>类型引用，而不管创建是是哪一个产品，都可以用<code>Product</code>接收，我们并不关心它们指向的是哪个工厂或者哪个产品，我们真正在意的只是它们能够做出正确的动作，仅此而已，这就是面向接口编程的好处。</p>
<br>

<h3 id="emsp-2-4-抽象工厂模式"><a href="#emsp-2-4-抽象工厂模式" class="headerlink" title="&emsp;2.4 抽象工厂模式"></a>&emsp;2.4 抽象工厂模式</h3><p>&emsp;&emsp;抽象共厂其实是基于工厂方法的，它的定义如下：</p>
<blockquote>
<p><strong>抽象工厂模式提供了一个接口，用来创建一组相关的对象，但是不需要明确指定这些对象的具体类型；</strong></p>
</blockquote>
<p>&emsp;&emsp;上面的定义说明，抽象工厂模式是用来生产多个相互联系的一组对象，但是却不需要关心这些对象的具体类型，这样就可以让这些对象与使用它们的代码解耦。而创建这一系列对象中的每一个对象的方法，使用的就是上面所述的工厂方法模式。也就是说，抽象工厂中包含了多个工厂方法。我们还是通过一个案例来看看吧。</p>
<p>&emsp;&emsp;假设我们要生产一辆车<code>Car</code>，为了简单，假设这个车由窗户和轮子两个零件组成，而不同类型的轮子和窗户可以组成不同的车。我们需要一些不同类型的轮子和窗户，下面定义了一个抽象类型<code>Wheel</code>表示轮子，而又定义了两个不同类型的轮子，一个是橡胶轮子，用来生产小汽车，而一个是木头轮子，用来生产木头车；</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 轮子抽象类</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Wheel</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 轮子的第一个产品：橡胶轮子</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RubberWheel</span> <span class="keyword">extends</span> <span class="title">Wheel</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 轮子的第二个产品：木头轮子</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">WoodenWheel</span> <span class="keyword">extends</span> <span class="title">Wheel</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;然后我们还需要窗户，于是定义了一个抽象类<code>Window</code>表示窗户，但是有玻璃窗户和木头窗户两种子类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 窗户抽象类</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Window</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 玻璃窗户</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">GlassWindow</span> <span class="keyword">extends</span> <span class="title">Window</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 木头窗户</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">WoodenWindow</span> <span class="keyword">extends</span> <span class="title">Window</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;然后我们需要有一个<code>Car</code>类表示车，这个<code>Car</code>由轮子和窗户组成：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 车</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 车轮</span></span><br><span class="line">    Wheel wheel;</span><br><span class="line">    <span class="comment">// 车窗</span></span><br><span class="line">    Window window;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Car</span><span class="params">(Wheel wheel, Window window)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.wheel = wheel;</span><br><span class="line">        <span class="keyword">this</span>.window = window;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;接下来我们就要考虑如何生产不同类型的车了。我们需要一个工厂，它可以生产轮子，也可以生产窗户，然后还能够将轮子和窗户组合起来，拼成一辆车。于是我们定义了一个车工厂，它有两个抽象方法，用来生产轮子和窗户，同时还有一个方法用来组装车。车的组装过程是固定的，就是将轮子和窗户组合起来，但是根据需要生产的车的不同，可能需要的零件不同，所以下面这个类只是定义了一种生产车的标准，但是具体如何生成车，需要用到哪些零件，这需要子类根据自己的功能去决定：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 造车工厂：需要生产一系列车需要的零件，组合成车</span></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">CarFactory</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 造轮子</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> Wheel <span class="title">createWheel</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 造窗户</span></span><br><span class="line">    <span class="function"><span class="keyword">abstract</span> Window <span class="title">createWindow</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 将零件组合成车，这个过程是固定的，所以不需要在子类中实现</span></span><br><span class="line">     <span class="function">Car <span class="title">createCar</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Wheel wheel = createWheel();</span><br><span class="line">        Window window = createWindow();</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Car(wheel, window);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;我们希望我们有两个工厂，一个生成小汽车，使用玻璃窗户和橡胶轮子；一个生成木头车，使用木头轮子和木头窗户。于是我们需要继承上面定义的生产车的标准<code>CarFactory</code>，然后选择创建自己需要的零件，如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 制造小汽车</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CompactCarFactory</span> <span class="keyword">extends</span> <span class="title">CarFactory</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function">Wheel <span class="title">createWheel</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 小汽车需要橡胶轮子</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> RubberWheel();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function">Window <span class="title">createWindow</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 徐爱汽车需要玻璃窗户</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> GlassWindow();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 制造木头车</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">WoodenCarFactory</span> <span class="keyword">extends</span> <span class="title">CarFactory</span></span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function">Wheel <span class="title">createWheel</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 木头车需要木头轮子</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> WoodenWheel();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function">Window <span class="title">createWindow</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 木头车需要木头窗户</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> WoodenWindow();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;于是现在，根据我们选择的工厂的不同，就能够生产不同类型的车子了，使用抽象工厂的代码如下。我们如果选择的是小汽车工厂，那调用生产车的接口，实际生产的就是玻璃窗户和橡胶轮子，组合出来的就是小汽车；如果我们选择的是木头车工厂，使用的就是木头车需要的零件，生产出来的是木头车。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 使用小汽车工厂创建小汽车</span></span><br><span class="line">    CarFactory f1 = <span class="keyword">new</span> CompactCarFactory();</span><br><span class="line">    Car compactCar = f1.createCar();</span><br><span class="line">    System.out.println(compactCar.wheel.getClass());</span><br><span class="line">    System.out.println(compactCar.window.getClass());</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 使用木头车工厂创建木头车</span></span><br><span class="line">    CarFactory f2 = <span class="keyword">new</span> WoodenCarFactory();</span><br><span class="line">    Car woodenCar = f2.createCar();</span><br><span class="line">    System.out.println(woodenCar.wheel.getClass());</span><br><span class="line">    System.out.println(woodenCar.window.getClass());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>&emsp;&emsp;不难看出，在抽象工厂中，生产每一个零件的方法，使用的其实就是工厂方法模式，而将这些工厂方法放到一起，生产一系列相关的对象，就成了抽象工厂。抽象工厂让我们在创建多个相关联对象的过程中与使用它们的代码解耦，我们不需要在意具体工厂的类型，因为它们都是它们公共父类的类型，我们只需要调用父类提供的接口，多态机制就会进行正确的子类操作。我们也不需要在意我们生产的是哪些对象，因为我们使用的是它们的父类型去引用，但是并不会影响实际的功能。如果我们需要修改某个工厂生产的对象，只需要修改某个工厂方法即可，而对于调用它的代码来说，不会存在任何的影响，因为接口和返回结果看起来都没有改变。</p>
<br>

<h2 id="三、总结"><a href="#三、总结" class="headerlink" title="三、总结"></a>三、总结</h2><p>&emsp;&emsp;工厂模式的主要目的就是将创建对象的逻辑与使用对象的代码解耦合，它遵循了针对接口编程的原则，实际上还遵循了依赖倒置原则。其实不仅仅是工厂模式，几乎所有的设计模式，其作用都是为了降低代码的耦合性，提高代码的复用性。</p>
<br>

<h2 id="四、参考"><a href="#四、参考" class="headerlink" title="四、参考"></a>四、参考</h2><ul>
<li>《Head First设计模式》</li>
</ul>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-03-29T13:41:43.273Z" itemprop="dateUpdated">2020-03-29 21:41:43</time>
</span><br>


        
        世界是个球，前方总有路！
        
    </div>
    
    <footer>
        <a href="http://tewuyiang.gitee.io/blog">
            <img src="/blog/img/avatar.jpg" alt="特务依昂">
            特务依昂
        </a>
    </footer>
</blockquote>

        


        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="tag">设计模式</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/&title=《设计模式（2）——工厂模式详解》 — 博客主页&pic=http://tewuyiang.gitee.io/blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/&title=《设计模式（2）——工厂模式详解》 — 博客主页&source=一个未来程序员的博客~~~" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《设计模式（2）——工厂模式详解》 — 博客主页&url=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/&via=http://tewuyiang.gitee.io/blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/%E6%B5%85%E6%9E%90B%E6%A0%91%E3%80%81B-%E6%A0%91%E4%BB%A5%E5%8F%8AMySQL%E4%B8%AD%E7%9A%84%E4%BD%BF%E7%94%A8/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">MySQL中的索引为什么使用B+树实现</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/%E5%B9%B6%E5%8F%91%E2%80%94%E2%80%94%E8%AF%A6%E7%BB%86%E4%BB%8B%E7%BB%8DCAS%E6%9C%BA%E5%88%B6/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">并发——详细介绍CAS机制</h4>
      </a>
    </div>
  
</nav>



    




















</article>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/blog/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>特务依昂 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/&title=《设计模式（2）——工厂模式详解》 — 博客主页&pic=http://tewuyiang.gitee.io/blog/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/&title=《设计模式（2）——工厂模式详解》 — 博客主页&source=一个未来程序员的博客~~~" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《设计模式（2）——工厂模式详解》 — 博客主页&url=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/&via=http://tewuyiang.gitee.io/blog" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://tewuyiang.gitee.io/blog/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%EF%BC%882%EF%BC%89%E2%80%94%E2%80%94%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F%E8%AF%A6%E8%A7%A3/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: false };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '人呢，怎么不见了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!欢迎回来!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
